package top.xuziyi.crawler.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.xuziyi.common.pojo.StockIndustry;
import top.xuziyi.crawler.dao.StockIndustryDao;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;

@Service
public class StockIndustryService {

    @Autowired
    private StockIndustryDao stockIndustryDao;

    @PersistenceContext
    protected EntityManager em;

    @Transactional
    public void batchSave(List<StockIndustry> list) {
        for (int i = 0; i < list.size(); i++) {
            if (stockIndustryDao.findByCode(list.get(i).getCode()) == 0) {
                em.persist(list.get(i));
            }
            if (i % 200 == 0) {
                em.flush();
                em.clear();
            }
        }
    }

}
